package mapdemo;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

public class TreeMapDemo {

    public static void main(String[] args) {
        Map map = new TreeMap();

        map.put("021","上海");
        map.put("020","广州");
        map.put("029","西安");
        map.put("010","北京");
        map.put("025","南京");
        map.put("028","成都");

        //map.put(null,"东京"); //错误键不能为null
        //map.put(null,null); //错误键不能为null
        map.put("null","东京");
        map.put("null",null);

        //遍历方式一：获取键的集合，再遍历值的集合。
        System.out.println("---------遍历方式一--------");
        Set set = map.keySet();

        Iterator it = set.iterator();

        while(it.hasNext()){
            Object key = it.next();
            Object value = map.get(key);
            System.out.println(key + "=" + value);
        }

        //遍历方式二：遍历实体的集合
        System.out.println("-----------遍历方式二---------");
        Set<Map.Entry> entities = map.entrySet();
        for(Map.Entry entry: entities){
            Object key = entry.getKey();
            Object value = entry.getValue();
            System.out.println(key + "=" + value);
        }

    }
}
